package cn.fourhundredandeighteen.service.impl;

import cn.fourhundredandeighteen.dao.StudentDao;
import cn.fourhundredandeighteen.dao.SubjectDao;
import cn.fourhundredandeighteen.dao.SubjectStudentDao;
import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.entity.Subject;
import cn.fourhundredandeighteen.service.StudentService;
import cn.fourhundredandeighteen.service.SubjectService;
import cn.fourhundredandeighteen.service.SubjectStudentService;
import cn.fourhundredandeighteen.utill.MysqlConnect;
import cn.fourhundredandeighteen.utill.NumberUtil;

import java.util.List;

/**
 * @description:
 * @author: liuyongqi
 * @time: 2020/12/17
 */
public class SubjectServiceImpl implements SubjectService {
    @Override
    public Subject getSubjectInfo(Long subjectId) {
        SubjectDao subjectDao= MysqlConnect.into().getMapper(SubjectDao.class);
        try {
            Subject subject = subjectDao.selectByPrimaryKey(subjectId);
            MysqlConnect.destroy();
            return subject;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Subject> getSubjectInfoList(List<Long> subjectIds) {
        SubjectDao subjectDao=MysqlConnect.into().getMapper(SubjectDao.class);
        try {
            List<Subject> subjects = subjectDao.selectList(subjectIds);
            MysqlConnect.destroy();
            return subjects;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Subject> getAll() {
        SubjectDao subjectDao=MysqlConnect.into().getMapper(SubjectDao.class);
        try {
            List<Subject> subjects = subjectDao.selectAll();
            MysqlConnect.destroy();
            return subjects;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public int upDataSubject(Subject subject) {
        SubjectDao subjectDao=MysqlConnect.into().getMapper(SubjectDao.class);
        int i = subjectDao.updateByPrimaryKeySelective(subject);
        if (i>0){
            return i;
        }else
            return 0;
    }

    @Override
    public List<Student> getStudentInfo(Long subjectId) {
        SubjectStudentService subjectStudentService=new SubjectStudentServiceImpl();
        try {
            //获取学生id信息
            List<Long> subjectToStudentList = subjectStudentService.getSubjectToStudentList(subjectId);
            System.out.println("test------------------:"+subjectToStudentList);
            if (subjectToStudentList.size()<=0)
                return null;
            //数据不为零的话去查找学生信息
            StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
            List<Student> students = studentDao.selectStudentList(subjectToStudentList);
            if (students==null)
                return null;
            MysqlConnect.destroy();
            return students;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public int delSubject(Long subjectId) {
        SubjectDao subjectDao=MysqlConnect.into().getMapper(SubjectDao.class);
        int i = subjectDao.deleteByPrimaryKey(subjectId);
        MysqlConnect.destroy();
        if (i>0)
            return i;
        else
            return 0;
    }

    @Override
    public Long saveSubject(Subject subject) {
        SubjectDao subjectDao=MysqlConnect.into().getMapper(SubjectDao.class);
        //自己生成id
        Long aLong = NumberUtil.genRandomNum(10);
        subject.setSubjectId(aLong);
        int i = subjectDao.insertSelective(subject);
        MysqlConnect.destroy();
        if (i>0L)
            return aLong;
        else
            return 0L;
    }
}
